package blogsys.demo.utils;

import java.io.Serializable;

/**
 *返回定义码
 * 2022.5.14
 * @author xp
 * @version 1.0
 */
public enum  ResultCode implements Serializable {
    /* 成功 */
    SUCCESS(200, "请求成功"),
    SUCCESS_register(200, "注册成功"),
    SUCCESS_login(200, "用户登录成功"),
    SUCCESS_logout(200, "用户退出成功"),
    SUCCESS_ADD(200, "信息添加成功"),

    /* 默认失败 */
    COMMON_FAIL(999, "请求失败"),

    /* 用户错误 */
    USER_NOT_LOGIN(2001, "用户未登录"),
    USER_FAIL_TOKEN_CHECK(2002, "验证失败"),
    USER_CREDENTIALS_ERROR(2003, "账号或密码错误"),
    USER_Register_ERROR(2004, "账号已被注册"),

    /* 业务错误 */
    NO_PERMISSION(3001, "当前账号没有此权限");
    private Integer code;
    private String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 根据code获取message
     *
     * @param code
     * @return
     */
    public static String getMessageByCode(Integer code) {
        for (ResultCode ele : values()) {
            if (ele.getCode().equals(code)) {
                return ele.getMessage();
            }
        }
        return null;
    }
}
